package com.xr.dropdown;

import com.xr.common.BusinessTypeVar;
import com.xr.common.CommUtil;
import com.xr.common.Tools;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.common.*;

/**
 * dsListener
 */
public class dsListener extends AbstractDatasetListener {
  public boolean beforeLoadData(Dataset dataset)
     throws Exception {
	  String dsName = dataset.parameters().getString("dsName");
		String sql = "";
			SqlDataset ds = (SqlDataset) dataset;
			DoradoContext context = DoradoContext.getContext();
			 
			if(dsName != null && dsName.equals("selectComDept_datasetSelect")) {
				sql=ds.getSql();
				sql += CommUtil.setConditionSQL(ds)+" "; 
				String deptLevel = dataset.parameters().getString("deptLevel");
				if(deptLevel != null && !deptLevel.equals("")){
					sql+=" and d.dept_level="+deptLevel;
				}
				ds.setSql(sql);
				 
			}
			if(dsName != null && dsName.equals("empWillOn_datasetQueryEmployee")) {
				
				sql="select * from " +
						"(select emp.employee_code,emp.employee_name,emp.employee_id,emp.ID_CARD_TYPE,emp.ID_CARD_NUM, com.company_name,dept.dept_name,oos.off_memo," +
						"oos.status,oos.work_start_date,oos.work_end_date,oos.onoff_station_id " +
						//"(select position_name from pb_position where position_id=oos.position_id)position_name," +
						//"round( Months_between(decode(WORK_END_DATE,null,sysdate,WORK_END_DATE), WORK_START_DATE))workLenght" +
						" from org_employee emp, org_onoff_station oos, org_company com,org_dept dept" +
						" where emp.employee_id = oos.employee_id" +
						"  and com.company_id = oos.company_id" +
						"  and com.company_id=dept.company_id" +
						"  and oos.dept_id=dept.dept_id" +
						" and not exists(select 1 from org_onoff_station oos where oos.employee_id=emp.employee_id and oos.status in(1,2) )";
				sql+=" union all ";
				sql+="select emp.employee_code,emp.employee_name,emp.employee_id,emp.ID_CARD_TYPE,emp.ID_CARD_NUM,null,null,null," +
				"null,null,null,null "+
						" from org_employee emp where not exists(select 1 from org_onoff_station oos where oos.employee_id=emp.employee_id )" +
						")emp where 1=1 ";
				sql += CommUtil.setConditionSQL(ds)+" "; 
				ds.setSql(sql);
				 
			}
			if(dsName != null && dsName.equals("empOn_datasetQueryEmployee")) {
				sql=ds.getSql();
				String bt = dataset.parameters().getString("bt");
				if(CommUtil.isAdmin()==false){
					if(bt != null && !bt.equals("")){
						sql+=CommUtil.getDataRightSql("oos", bt);
					}
				}
				sql += CommUtil.setConditionSQL(ds)+" "; 
				ds.setSql(sql);
				 
			}	
			if(dsName != null && dsName.equals("selectUserForSys_datasetQueryEmployee")) {
				sql=ds.getSql();
				sql += CommUtil.setConditionSQL(ds)+" "; 
				ds.setSql(sql);
				 
			}
			if(dsName != null && dsName.equals("batchAjustWf_datastSelectEmp")) {
				sql=ds.getSql();
				String hritemid=Tools.toStr( ds.parameters().getString("HRITEMS_ID"));
				String ifnull=Tools.toStr( ds.parameters().getString("IF_NULL"));
				if(!hritemid.equals("")){
					if(ifnull.equals("1"))
					  sql+=" and exists(select 1 from wf_ecfi we where we.onoff_station_id=oos.onoff_station_id and we.hritems_id="+hritemid+" and sysdate between we.start_date and we.end_date and nvl(money,0)>0)";
					else
						sql+=" and not exists(select 1 from wf_ecfi we where we.onoff_station_id=oos.onoff_station_id and we.hritems_id="+hritemid+" and sysdate between we.start_date and we.end_date and nvl(money,0)>0)";
				}
				sql += CommUtil.setConditionSQL(ds)+" "; 
				sql += CommUtil.getDataRightSql("oos", BusinessTypeVar.hr_personnel_dsListener_onOffList_datasetOnOffList);
				ds.setSql(sql);
			}
    return true;
  }

}
